<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Authentication
{
    private $CI;
    //不需权限验证的类
    public $logout_page = array('login','ajax','logout','cron');

    public function __construct()
    {
        $this->CI =& get_instance();
    }

    public function check_login()
    {	
    	$this->CI->load->helper('url');
    	
    	if($this->CI->router->directory === 'admin/')
    	{
    	    $this->CI->load->library('CT_auth');
    	    $class = $this->CI->router->class;
    	    $method = $this->CI->router->method;
    
    		//权限验证开始
            if( !in_array($class, $this->logout_page)  )
    		{
    		    $is_login = $this->CI->ct_auth->is_admin_login();
    		    if( !$is_login ){
    		        $get = $this->CI->input->get() ? $this->CI->input->get() : array();
    		        $r_url = base_url() . uri_string() ."?". http_build_query($get);
    		        redirect('/admin/login'.'?url='. urlencode($r_url));
    		    }
    		    
    			$this->CI->load->library('AdminAction');
                //赋予超级管理员最高权限
    			if($this->CI->adminaction->role_name === '超级管理员') return;
    			//排除管理员首页
            	if( $class=='index' && $method=='index' ) return;
    			//排除管理员修改密码的方法
    			if($class=='manage' && $method=='set') return;
    			//排除所有的ajax请求
    			if($this->CI->input->is_ajax_request()) return;
    		
            	$my_action = $this->CI->router->directory. '/'. $class.'/'.$method;
    	        $this->CI->adminaction->link = $my_action;
    	        $this->CI->adminaction->check_current_role_permissions();
            }
    	}
    	
    	if($this->CI->router->directory === 'nursery/')
    	{
    	    $this->CI->load->library('CT_auth_nursery');
    	    $class = $this->CI->router->class;
    	    $method = $this->CI->router->method;
    	
    	    //权限验证开始
    	    if( !in_array($class, $this->logout_page)  )
    	    {
    	        $is_login = $this->CI->ct_auth_nursery->is_nursery_login();
    	        if( !$is_login ){
    	            $get = $this->CI->input->get() ? $this->CI->input->get() : array();
    	            $r_url = base_url() . uri_string() ."?". http_build_query($get);
    	            redirect('/nursery/login'.'?url='. urlencode($r_url));
    	        }
    	
    	        $this->CI->load->library('NurseryAdminAction');
    	        //赋予超级管理员最高权限
    	        if($this->CI->nurseryadminaction->role_name === '超级管理员') return;
    	        //排除管理员首页
    	        if( $class=='index' && $method=='index' ) return;
    	        //排除管理员修改密码的方法
    	        if($class=='admin' && $method=='set') return;
    	        //排除所有的ajax请求
    	        if($this->CI->input->is_ajax_request()) return;
    	
    	        $my_action = $this->CI->router->directory. '/'. $class.'/'.$method;
    	        $this->CI->nurseryadminaction->link = $my_action;
    	        $this->CI->nurseryadminaction->check_current_role_permissions();
    	    }
    	}

    }

}